package com.bzkj.inventory.service.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.inventory.service.NccloudSaleoutSignService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NccloudSaleoutSignServiceImpl implements NccloudSaleoutSignService {
    @Value("${ncc.baseUrl}")
    private String baseUrl;
    @Override
    public AjaxResult sign(List<String> cgeneralhid) {
        AjaxResult ajaxResult = new AjaxResult();
        String message="";
        for (String s : cgeneralhid) {
            HttpResponse response = methodExecute(s);
            if (response.isOk()) {
                String responseBody = response.body();
                JSONObject entries = JSONUtil.parseObj(responseBody);
                Boolean success = entries.getBool("success");
                message = entries.getStr("message");
                if (!success){
                    return ajaxResult.put("tag",false).put("msg",message);
                }
            }
        }
        return ajaxResult.put("tag",true).put("msg",message);
    }

    public HttpResponse methodExecute(String s) {
        // 构造JSON数组
        String[] array = new String[]{s};
        String jsonBody = JSONUtil.toJsonStr(array);

        // 添加请求头
        HttpResponse response = HttpUtil.createPost(baseUrl)
                .header("SourceSystem", "WMS")
                .header("TargetSystem", "NCC")
                .header("ServiceName", "S011010001A")
                .header("Content-Type", "application/json")
                .header("path", "/nccloud/api/ic/saleout/sign")
                .body(jsonBody)
                .execute();
        return response;
    }
}
